﻿using System;
using System.Collections.Generic;

using System.Configuration;

using System.IO;

using System.Windows;
using System.Windows.Media.Animation;

using System.Threading;
using System.Windows.Threading;

using EagleJournalBE.Util;
using EagleJournalBE.Database;

using EagleJournal.Components;

namespace EagleJournal
{
    public partial class App : Application
    {
        protected override  void    OnStartup                               (StartupEventArgs e)
        {
            Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), 
                new FrameworkPropertyMetadata { DefaultValue = 80 });

            Logger.WriteInfo(LoggingType.INFO, this, "Attaching exception listeners...");

            AppDomain.CurrentDomain.UnhandledException
                += new UnhandledExceptionEventHandler                                   
                    (CurrentDomain_UnhandledException);

            Application.Current.DispatcherUnhandledException
                += new DispatcherUnhandledExceptionEventHandler
                    (Current_DispatcherUnhandledException);

            Logger.WriteInfo(LoggingType.INFO, this, "Attachment complete! Starting MySQL server connection...");

            string dpPath   = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mysqlpword.rhe");
            string password = string.Empty;

            if (File.Exists(dpPath))
            {
                using (BinaryReader bread = new BinaryReader(File.OpenRead(dpPath)))
                {
                    password = bread.ReadString();
                }
            }
            else
            {
                WindowSQLPassword pWordWindow = new WindowSQLPassword();

                if (pWordWindow.ShowDialog().Value)
                {
                    password = pWordWindow.password;

                    using (BinaryWriter write = new BinaryWriter(File.OpenWrite(dpPath)))
                    {
                        write.Write(password);
                    }

                    MessageBox.Show("Password saved! Re-open Eagle Journal to log in.", "Eagle Journal", MessageBoxButton.OK, MessageBoxImage.Information);

                    Application.Current.Shutdown();
                }
            }

            try
            {
                DatabaseConnector.Connect("root", password);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Eagle Journal cannot connect to the database, the application will now shutdown.", "Eagle Journal", MessageBoxButton.OK, MessageBoxImage.Error);

                Logger.WriteInfo(LoggingType.STOP, this, ex.ToString());

                Application.Current.Shutdown();
            }

            base.OnStartup(e);
        }
        protected override  void    OnExit                                  (ExitEventArgs e)
        {
            DatabaseConnector.Disconnect();

            base.OnExit(e);
        }

        void                        CurrentDomain_UnhandledException        (object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show(((Exception)e.ExceptionObject).ToString(), "Eagle Journal app panic", MessageBoxButton.OK, MessageBoxImage.Error);
            Logger.WriteInfo(LoggingType.STOP, this, ((Exception)e.ExceptionObject).ToString());

            Application.Current.Shutdown();
        }
        void                        Current_DispatcherUnhandledException    (object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            Logger.WriteInfo(LoggingType.STOP, this, e.Exception.ToString());

            e.Handled = true;
        }
    }
}
